import {Component} from '@angular/core';
import {
  NavController,
  NavParams,
  ViewController,
  ModalController,
  LoadingController,
  ToastController, Platform
} from 'ionic-angular';
import {ModifyTextPage} from '../modify-text/modify-text'
import {ModifyPhotoPage} from '../modify-photo/modify-photo';
import {ModifySexPage} from '../modify-sex/modify-sex'
import {ModifyAgePage} from '../modify-age/modify-age'
import {
  Http,
  Headers,
  RequestOptions,
  URLSearchParams
} from '@angular/http';
import {
  baseCommon
} from '../../common/baseCommon';
import {Storage} from '@ionic/storage'
import {AppConfig} from '../../app/app.config';

/**
 * Generated class for the ModifyDataPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-modify-data',
  templateUrl: 'modify-data.html',
})
export class ModifyDataPage{

  listData: any;
  phone: any;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public viewCtrl: ViewController,
              public http: Http,
              public storage: Storage,
              public loadingCtrl: LoadingController,
              public toastCtrl: ToastController,
              public platform: Platform,
              public modalCtrl: ModalController) {
    this.listData = navParams.data.listData;
    console.log(this.listData);

  }

  ionViewDidEnter() {
    AppConfig.viewCtrl = this.viewCtrl;
  }

  ionViewDidLoad() {
    this.storage.get('phone').then(val => {
      this.phone = val;
    })
  }

  goModifyPhoto() {
    let model = this.modalCtrl.create(ModifyPhotoPage);
    model.onDidDismiss(data => {
      AppConfig.viewCtrl = this.viewCtrl;
      this.storage.get('sessionID').then(val => {
        baseCommon.getInfo(this.http,this.phone,val)
        .then(data=>{
          this.listData.imageURL = data.imageURL;
        })
      })
    })
    model.present();
  }

  goModifySex() {
    let model = this.modalCtrl.create(ModifySexPage, {'text': '性别'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        if (data == 1) {
          this.listData['sex'] = '男';
        } else if (data == 2) {
          this.listData['sex'] = '女';
        } else {
          this.listData['sex'] = '保密';
        }
      }
      AppConfig.viewCtrl = this.viewCtrl;
    })
    model.present();
  }

  goModifyAge() {
    let model = this.modalCtrl.create(ModifyAgePage, {'text': '年龄'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['age'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;
    })
    model.present();
  }

  goModifyNickname() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '昵称'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['nickname'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;
    })
    model.present();
  }

  goModifyRemarks() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '签名'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['remarks'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;

    })
    model.present();
  }

  goModifySkill() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '技能'});
    model.dismiss(data => {
      AppConfig.viewCtrl = this.viewCtrl;
    })
    model.present();
  }

  goModifyJob() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '职业'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['job'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;

    })
    model.present();
  }

  goModifyHometown() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '家乡地址'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['hometown'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;

    })
    model.present();
  }

  goModifySchool() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '学校'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['school'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;

    })
    model.present();
  }

  goModifyCompany() {
    let model = this.modalCtrl.create(ModifyTextPage, {'text': '就职公司'});
    model.onDidDismiss(data => {
      if (data != undefined) {
        this.listData['company'] = data;
      }
      AppConfig.viewCtrl = this.viewCtrl;

    })
    model.present();
  }

  savaInof() {
    console.log('尝试保存')
    this.storage.get('sessionID').then(val => {
      this.updateInfo(AppConfig.httpLoc + '/info/update', val, this.listData['nickname'], this.listData['sex'], this.listData['age'], this.listData['job'], this.listData['hometown'], this.listData['school'], this.listData['company'], this.listData['remarks']);
    })
  }


  dismiss() {
    this.viewCtrl.dismiss()
  }

  /**
   * 更新个人信息
   *
   * @param {any} apiUrl
   * @param {any} phone
   * @param {any} password
   * @memberof ModifyDataPage
   */
  updateInfo(apiUrl, sessionID, nickname, sex, age, job, hometown, school, company, remarks) {
    var loading = baseCommon.showLoading(this.loadingCtrl, "慢慢地保存中...");
    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    let options = new RequestOptions({
      headers: headers,
    });

    let pramas: URLSearchParams = new URLSearchParams();

    pramas.set('sessionID', sessionID);
    pramas.set('nickname', nickname);
    pramas.set('sex', (sex === "男") ? (String)(1) : ((sex === "女") ? (String)(2) : (String)(0)));
    pramas.set('age', age);
    pramas.set('job', job);
    pramas.set('hometown', hometown);
    pramas.set('school', school);
    pramas.set('company', company);
    pramas.set('remarks', remarks);

    let getData: any;

    this.http.post(apiUrl, pramas, options)
      .toPromise()
      .then(data => {
        loading.dismissAll();
        baseCommon.showToast(this.toastCtrl, '保存成功，你又可以去驰骋疆场、策马奔腾了...得儿驾...');
        getData = data.json();
        if (getData.message == "成功") {
          this.storage.set('nickname', nickname);
          setTimeout(val => {
            this.dismiss();
          }, 1000);
        } else {
          baseCommon.showToast(this.toastCtrl, "保存失败:(");
        }
      }, err => {
        baseCommon.showToast(this.toastCtrl, "保存失败:(");
      })
  }
}
